﻿#include "Windows.h"
#include "Commdlg.h"

#include "zyColorDialog.h"


BlackMoonUI::zyColorDialog::zyColorDialog()
{
	SetMember(L"this", (LONG_PTR)this);
}
BlackMoonUI::zyColorDialog::~zyColorDialog()
{
	COLORREF* custColors = (COLORREF*)GetMember(L"custColors");
	if (custColors != nullptr) {
		delete custColors;
	}
	CHOOSECOLOR* cc = (CHOOSECOLOR*)GetMember(L"pChooseColor");
	if (cc != nullptr) {
		delete cc;
	}
}
LRESULT CALLBACK BlackMoonUI::zyColorDialog::CCHookProc(HWND hdlg, UINT message, WPARAM wParam, LPARAM lParam) {
	if (message == WM_INITDIALOG) {
		CHOOSECOLOR* cc = reinterpret_cast<CHOOSECOLOR*>(lParam);
		zyColorDialog* Dialog = reinterpret_cast<zyColorDialog*>(cc->lCustData);
		if (Dialog) {
			Dialog->SetMember(L"hWnd", (LONG_PTR)hdlg);
			//窗口数据绑定[hdlg] = Dialog;
			zyEnv::getInstance().registerWnd(hdlg, Dialog->getObject());
			Dialog->_MoveToStartPosition(hdlg);
		}
	}
	//zyBase* this_ = 窗口数据绑定[hdlg];// 如果找到了对应的 zyBase 实例，则尝试处理消息
	if (zyObject* obj = zyEnv::getInstance().getWndObject(hdlg)) {
		BOOL bHand = false;
		LRESULT ret = obj->callWndProc(hdlg, message, wParam, lParam, &bHand);
		if (bHand) return ret;
	}
	return 0;
}
bool BlackMoonUI::zyColorDialog::打开(HWND 参数_父窗口)
{
	CHOOSECOLOR* pChooseColor = (CHOOSECOLOR*)GetMember(L"pChooseColor");
	if (pChooseColor == nullptr) {
		return false;
	}
	pChooseColor->hwndOwner = 参数_父窗口;
	//pChooseColor->hInstance = 参数_父窗口;
	return ChooseColor(pChooseColor) != 0;
}
void BlackMoonUI::zyColorDialog::初始化(int 参数_选项, int 参数_初始颜色, std::vector<COLORREF>* 参数_自定义颜色组) {
	COLORREF* custColors = (COLORREF*)GetMember(L"custColors");
	if (custColors == nullptr) {
		custColors = new COLORREF[16];
		SetMember(L"custColors", (LONG_PTR)custColors);
		for (int i = 0; i < 16; i++) {
			custColors[i] = RGB(255, 255, 255);
		}
		/* 参数_自定义颜色组.resize(16);
		SetMember ("custColors",(vint)&参数_自定义颜色组[0]);
		for(int i = 0;i < 16 ;i++){
			参数_自定义颜色组[i] = RGB(255,255,255);
		} */
	}
	CHOOSECOLOR* cc = (CHOOSECOLOR*)GetMember(L"pChooseColor");
	if (cc != nullptr) {
		delete cc;
	}
	cc = new CHOOSECOLOR;
	ZeroMemory(cc, sizeof(CHOOSECOLOR)); // 初始化结构体，避免未定义行为

	cc->lStructSize = sizeof(CHOOSECOLOR);
	cc->Flags = 参数_选项 | CC_ENABLEHOOK | CC_RGBINIT;
	cc->lpfnHook = (LPCCHOOKPROC)BlackMoonUI::zyColorDialog::CCHookProc;
	cc->lCustData = reinterpret_cast<LPARAM>(this);
	cc->rgbResult = 参数_初始颜色;
	if (参数_自定义颜色组 && 参数_自定义颜色组->size() >= 16)
		cc->lpCustColors = (DWORD*)参数_自定义颜色组->data();
	else
		cc->lpCustColors = custColors;
	SetMember(L"pChooseColor", (LONG_PTR)cc);
}
COLORREF BlackMoonUI::zyColorDialog::取颜色值() {
	CHOOSECOLOR* pChooseColor = (CHOOSECOLOR*)GetMember(L"pChooseColor");
	return pChooseColor ? pChooseColor->rgbResult : RGB(0, 0, 0);
}
int BlackMoonUI::zyColorDialog::取选项() {//"颜色对话框_"开头常量组合
	CHOOSECOLOR* pChooseColor = (CHOOSECOLOR*)GetMember(L"pChooseColor");
	return pChooseColor ? pChooseColor->Flags : 0;
}
void BlackMoonUI::zyColorDialog::取自定义颜色组(std::vector<COLORREF>& 参数_自定义颜色组) {//返回保存的自定义颜色数组
	CHOOSECOLOR* pChooseColor = reinterpret_cast<CHOOSECOLOR*>(GetMember(L"pChooseColor"));
	参数_自定义颜色组.resize(16);
	if (pChooseColor) {
		std::copy_n(pChooseColor->lpCustColors, 16, 参数_自定义颜色组.begin());
	}	
	/*for (int i = 0; i < 16; i++) {
		参数_自定义颜色组[i] = static_cast<COLORREF>(pChooseColor->lpCustColors[i]);
	}*/
}